package controllers;

import java.util.List;

import models.UserConfig;
import play.Logger;
import play.mvc.Controller;
import ch.bfh.abch.rems.REMSMulticastController;
import ch.bfh.abch.rems.sender.tinkerforge.TFMulticastSender;
import ch.bfh.abcm.rems.konfiguration.UpdateUserConfigMessage;
import controllers.validation.REMSValidationController;

public class UserConfigController extends Controller {

    public static void index() {
        List<models.UserConfig> userConfigList = models.UserConfig.findAll();
        render(userConfigList);
    }

    public static void setPropertyValue(String key, String value) {
        String TFValue = value;
        UserConfig userconfig = UserConfig.getProperty(key);
        userconfig.setPropertyValue(value);
        userconfig.save();
        Logger.debug("Key: %s,  userValue: %s TFValue %s", key, value, TFValue);
        sendUserconfigUpdateToOtherNodes(key, value);
        index();
    }

    private static void sendUserconfigUpdateToOtherNodes(String key, String value) {
        UpdateUserConfigMessage updateUserConfigMessage = new UpdateUserConfigMessage(key, value);
        TFMulticastSender multicastSender = new REMSMulticastController().getMulticastSender();
        multicastSender.sendUpdateUserConfigMessage(updateUserConfigMessage);
    }

    public static void runValidationjob() {
        new REMSValidationController().startValidationJob();
        index();
    }

    public static void insertDefaultUserConfig() {
        UserConfig.deleteAll();
        new UserConfig(UserConfig.TEMPERATURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.MOISTURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.TEMPERATURE_IR_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.HUMIDITY_MAX_DIVERGENT, "400", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.AMBIENT_MAX_DIVERGENT, "1300", "Sensorfehertoleranz").save();
        // Maximum age of sensor values (Aktualität)
        new UserConfig(UserConfig.SENSOR_MAX_TIME_DIFF_MS, "5000", "10 ms  set letztem Wert (500 = 5000)").save();
        // Maximum and minimum of effective values (Schwellwerte)
        new UserConfig(UserConfig.TEMPERATURE_MAX_VALUE_USER, "3500", "TemperaturObergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_MIN_VALUE_USER, "170", "TemperaturUntergrenze").save();
        new UserConfig(UserConfig.MOISTURE_MAX_VALUE_USER, "3500", "TemperaturObergrenze").save();
        new UserConfig(UserConfig.MOISTURE_MIN_VALUE_USER, "170", "TemperaturUntergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_IR_MAX_VALUE_USER, "3500", "TemperaturObergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_IR_MIN_VALUE_USER, "170", "TemperaturUntergrenze").save();
        new UserConfig(UserConfig.AMBIENT_MAX_VALUE_USER, "2000", "LuxObergrenze").save();
        new UserConfig(UserConfig.AMBIENT_MIN_VALUE_USER, "300", "LuxUntergrenze").save();
        new UserConfig(UserConfig.HUMIDITY_MAX_VALUE_USER, "535", "LuftfeuchtigkietObergrenze").save();
        new UserConfig(UserConfig.HUMIDITY_MIN_VALUE_USER, "100", "LuftfeuchtigkietUntergrenze").save();
        index();
    }
}